<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>数字签名 | 一枚前端小菜鸟的博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="数字签名的实现原理监听鼠标按下事件，鼠标移动事件，鼠标放开事件鼠标按下：标志绘画开始，isDraw&#x3D;true,记录当前的坐标鼠标移动：如果isDraw&#x3D;true，开始绘画，连接开始点和结束点画一条直线，然后标记结束点为开始点，重复绘画过程鼠标放开or超出绘画区域：绘画结束canvas基础 、 beginPath和closePath  12345678910111213141516171819202">
<meta property="og:type" content="article">
<meta property="og:title" content="数字签名">
<meta property="og:url" content="https://gitee.com/weiyayun1995/weiyayun1995.git/2021/06/18/%E6%95%B0%E5%AD%97%E7%AD%BE%E5%90%8D/index.html">
<meta property="og:site_name" content="一枚前端小菜鸟的博客">
<meta property="og:description" content="数字签名的实现原理监听鼠标按下事件，鼠标移动事件，鼠标放开事件鼠标按下：标志绘画开始，isDraw&#x3D;true,记录当前的坐标鼠标移动：如果isDraw&#x3D;true，开始绘画，连接开始点和结束点画一条直线，然后标记结束点为开始点，重复绘画过程鼠标放开or超出绘画区域：绘画结束canvas基础 、 beginPath和closePath  12345678910111213141516171819202">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fe.che300.com/easymock/upload/2020/03/02/41ee2941ed6455459dbfdfa526a39a4a.png">
<meta property="og:image" content="https://fe.che300.com/easymock/upload/2020/03/02/7414cbfdceae95f6052ab5729ef3f252.png">
<meta property="og:image" content="https://fe.che300.com/easymock/upload/2020/03/02/833f4ee1ce9fe7a273aad114f7903653.png">
<meta property="og:image" content="https://fe.che300.com/easymock/upload/2020/03/02/9bd0576949c8affb90a994c0a8147762.png">
<meta property="og:image" content="https://fe.che300.com/easymock/upload/2020/03/02/4595137ebbed32c68a4a17e7f991f0d4.png">
<meta property="og:image" content="https://fe.che300.com/easymock/upload/2020/03/02/d0eb57fb482da0efbeed67d7d074ca27.png">
<meta property="og:image" content="https://fe.che300.com/easymock/upload/2020/03/02/74552325c2f5154fd72713ea7f93e4ce.png">
<meta property="og:image" content="https://fe.che300.com/easymock/upload/2020/03/02/799df615aa8e7f1753d9f95f1815223b.png">
<meta property="article:published_time" content="2021-06-18T06:22:18.000Z">
<meta property="article:modified_time" content="2021-06-18T06:22:37.786Z">
<meta property="article:author" content="yywei">
<meta property="article:tag" content="菜鸟,前端,js,react,vue">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fe.che300.com/easymock/upload/2020/03/02/41ee2941ed6455459dbfdfa526a39a4a.png">
  
    <link rel="alternate" href="/atom.xml" title="一枚前端小菜鸟的博客" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">一枚前端小菜鸟的博客</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">yywei的遇坑记录</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS 订阅"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="搜索"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://gitee.com/weiyayun1995/weiyayun1995.git"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-数字签名" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2021/06/18/%E6%95%B0%E5%AD%97%E7%AD%BE%E5%90%8D/" class="article-date">
  <time class="dt-published" datetime="2021-06-18T06:22:18.000Z" itemprop="datePublished">2021-06-18</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      数字签名
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="数字签名的实现"><a href="#数字签名的实现" class="headerlink" title="数字签名的实现"></a>数字签名的实现</h2><h3 id="原理"><a href="#原理" class="headerlink" title="原理"></a>原理</h3><p>监听鼠标按下事件，鼠标移动事件，鼠标放开事件<br>鼠标按下：标志绘画开始，isDraw=true,记录当前的坐标<br>鼠标移动：如果isDraw=true，开始绘画，连接开始点和结束点画一条直线，然后标记结束点为开始点，重复绘画过程<br>鼠标放开or超出绘画区域：绘画结束<br><a target="_blank" rel="noopener" href="https://www.runoob.com/w3cnote/html5-canvas-intro.html">canvas基础</a> 、 <a target="_blank" rel="noopener" href="https://www.cnblogs.com/huaan011/p/5651806.html">beginPath和closePath</a> </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line"></span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta charset&#x3D;&quot;utf-8&quot;&gt;</span><br><span class="line">    &lt;title&gt;数字签名&lt;&#x2F;title&gt;</span><br><span class="line">&lt;&#x2F;head&gt;</span><br><span class="line">&lt;style&gt;</span><br><span class="line">    body &#123;</span><br><span class="line">        display: flex;</span><br><span class="line">        justify-content: center;</span><br><span class="line">    &#125;</span><br><span class="line">    .header &#123;</span><br><span class="line">        display: flex;</span><br><span class="line">        justify-content: space-between;</span><br><span class="line">    &#125;</span><br><span class="line">    .canvas &#123;</span><br><span class="line">        &#x2F;* width: 600px;</span><br><span class="line">        height:300px; *&#x2F;</span><br><span class="line">        margin-top: 20px;</span><br><span class="line">        border: 1px solid #ddd;</span><br><span class="line">    &#125;</span><br><span class="line">    .image &#123;</span><br><span class="line">        border: 1px solid #ddd;</span><br><span class="line">    &#125;</span><br><span class="line">&lt;&#x2F;style&gt;</span><br><span class="line"></span><br><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;div class&#x3D;&quot;container&quot;&gt;</span><br><span class="line">        &lt;div class&#x3D;&quot;header&quot;&gt;</span><br><span class="line">            &lt;span class&#x3D;&quot;title&quot;&gt;数字签名&lt;&#x2F;span&gt;</span><br><span class="line">            &lt;div class&#x3D;&quot;buttons&quot;&gt;</span><br><span class="line">                &lt;button class&#x3D;&quot;save&quot;&gt;保存&lt;&#x2F;button&gt;</span><br><span class="line">                &lt;button class&#x3D;&quot;reset&quot;&gt;重置&lt;&#x2F;button&gt;</span><br><span class="line">            &lt;&#x2F;div&gt;</span><br><span class="line">        &lt;&#x2F;div&gt;</span><br><span class="line">        &lt;div&gt;</span><br><span class="line">            &lt;!-- 一定要设置canvas的宽高，只是通过css设置宽高是不行的会产生偏移 --&gt;</span><br><span class="line">            &lt;canvas class&#x3D;&quot;canvas&quot;  width&#x3D;&quot;600&quot;  height&#x3D; &quot;300&quot;&gt;&lt;&#x2F;canvas&gt;</span><br><span class="line">        &lt;&#x2F;div&gt;</span><br><span class="line">        &lt;div&gt;</span><br><span class="line">            &lt;p&gt;保存的图片为：&lt;&#x2F;p&gt;</span><br><span class="line">            &lt;img class&#x3D;&quot;image&quot;&#x2F;&gt;</span><br><span class="line">        &lt;&#x2F;div&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">    </span><br><span class="line">    &lt;script&gt;</span><br><span class="line">        var save &#x3D; document.querySelector(&#39;.save&#39;);</span><br><span class="line">        var reset &#x3D; document.querySelector(&#39;.reset&#39;)</span><br><span class="line">        var image &#x3D; document.querySelector(&#39;.image&#39;)</span><br><span class="line">        var canvas &#x3D; document.querySelector(&#39;.canvas&#39;)</span><br><span class="line">        var canvasContext &#x3D; canvas.getContext(&#39;2d&#39;)</span><br><span class="line">        var isDraw &#x3D; false; &#x2F;&#x2F; 标志是否要绘画</span><br><span class="line">        var startPos &#x3D; &#123;x: 0,y:0&#125; &#x2F;&#x2F; 开始坐标</span><br><span class="line">        canvasContext.strokeStyle &#x3D; &#39;#000&#39; &#x2F;&#x2F; 设置画笔颜色</span><br><span class="line">        canvasContext.lineWidth &#x3D; 1; &#x2F;&#x2F; 设置画笔的粗细</span><br><span class="line">        canvas.onmousedown &#x3D; function(ev)&#123; </span><br><span class="line">            ev.preventDefault();</span><br><span class="line">            &#x2F;&#x2F; 开始画</span><br><span class="line">            canvasContext.beginPath();</span><br><span class="line">            isDraw &#x3D; true;</span><br><span class="line">            startPos &#x3D; &#123;</span><br><span class="line">                x: ev.offsetX,</span><br><span class="line">                y: ev.offsetY</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        canvas.onmousemove &#x3D; function(ev)&#123;</span><br><span class="line">            ev.preventDefault();</span><br><span class="line">            if(isDraw)&#123;</span><br><span class="line">                &#x2F;&#x2F; canvas中的绘制方法（如stroke,fill），都会以“上一次beginPath”之后的所有路径为基础进行绘制</span><br><span class="line">                canvasContext.beginPath(); &#x2F;&#x2F; 新建一条path</span><br><span class="line">                canvasContext.moveTo(startPos.x,startPos.y) &#x2F;&#x2F; 把画笔移动到指定的坐标</span><br><span class="line">                canvasContext.lineTo(ev.offsetX,ev.offsetY); &#x2F;&#x2F;绘制一条从当前位置到指定坐标的直线.</span><br><span class="line">                canvasContext.stroke();&#x2F;&#x2F;绘制路径</span><br><span class="line">                canvasContext.closePath() &#x2F;&#x2F; 闭合路径</span><br><span class="line">                startPos.x &#x3D; ev.offsetX;</span><br><span class="line">                startPos.y &#x3D; ev.offsetY</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        canvas.onmouseup &#x3D; function(ev)&#123;</span><br><span class="line">            ev.preventDefault();</span><br><span class="line">            isDraw &#x3D; false;</span><br><span class="line">            startPos &#x3D; &#123;x: 0,y:0&#125;</span><br><span class="line">        &#125;</span><br><span class="line">        canvas.onmouseout &#x3D; function(ev)&#123;</span><br><span class="line">            ev.preventDefault();</span><br><span class="line">            isDraw &#x3D; false;</span><br><span class="line">            canvasContext.closePath()</span><br><span class="line">        &#125;</span><br><span class="line">        &#x2F;&#x2F; 保存成图片</span><br><span class="line">        save.onclick &#x3D; function()&#123;</span><br><span class="line">            console.log(canvas)</span><br><span class="line">            var dataUrl &#x3D; canvas.toDataURL()</span><br><span class="line">            console.log(dataUrl)</span><br><span class="line">            image.src &#x3D; dataUrl;</span><br><span class="line">        &#125;</span><br><span class="line">        &#x2F;&#x2F; 重置</span><br><span class="line">        reset.onclick&#x3D; function()&#123;</span><br><span class="line">            canvasContext.clearRect(0,0,canvas.width,canvas.height)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &lt;&#x2F;script&gt;</span><br><span class="line">&lt;&#x2F;body&gt;</span><br><span class="line"></span><br><span class="line">&lt;&#x2F;html&gt;</span><br></pre></td></tr></table></figure>
<p><img src="https://fe.che300.com/easymock/upload/2020/03/02/41ee2941ed6455459dbfdfa526a39a4a.png"><br>功能虽然实现了，但是这里有个问题，那就是画的线有锯齿<br><strong>原因</strong>：<br>mousemove是隔一段时间采集一个点，而lienTo()就是两点之间的连线，多个线段之间连接就会出现锯齿状<br><img src="https://fe.che300.com/easymock/upload/2020/03/02/7414cbfdceae95f6052ab5729ef3f252.png"></p>
<p><strong>解决：</strong><br>使用二次贝塞尔曲线<br>quadraticCurveTo(控制点.X, 控制点.Y, 终点.X,终点的.Y)<br><img src="https://fe.che300.com/easymock/upload/2020/03/02/833f4ee1ce9fe7a273aad114f7903653.png"><br>从上图可以看出如果要使用二倍贝塞尔曲线，至少需要三个点，而现在只有两个点（开始点和结束点），那么如何设置开始点、控制点、结束点呢？如下所示：<br>现在有A、B、C三个点，计算出B和C点的中点B1作为结束点，B点作为控制点，A点作为开始点，<br><img src="https://fe.che300.com/easymock/upload/2020/03/02/9bd0576949c8affb90a994c0a8147762.png"></p>
<p>以B1作为开始点， C为控制点，计算C和D的中点C1作为结束点<img src="https://fe.che300.com/easymock/upload/2020/03/02/4595137ebbed32c68a4a17e7f991f0d4.png"></p>
<p>重复上面的步骤就会形成一条曲线<br><img src="https://fe.che300.com/easymock/upload/2020/03/02/d0eb57fb482da0efbeed67d7d074ca27.png"></p>
<p><strong>具体实现：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line"></span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta charset&#x3D;&quot;utf-8&quot;&gt;</span><br><span class="line">    &lt;title&gt;数字签名&lt;&#x2F;title&gt;</span><br><span class="line">&lt;&#x2F;head&gt;</span><br><span class="line">&lt;style&gt;</span><br><span class="line">    body &#123;</span><br><span class="line">        display: flex;</span><br><span class="line">        justify-content: center;</span><br><span class="line">    &#125;</span><br><span class="line">    .header &#123;</span><br><span class="line">        display: flex;</span><br><span class="line">        justify-content: space-between;</span><br><span class="line">    &#125;</span><br><span class="line">    .canvas &#123;</span><br><span class="line">        &#x2F;* width: 600px;</span><br><span class="line">        height:300px; *&#x2F;</span><br><span class="line">        margin-top: 20px;</span><br><span class="line">        border: 1px solid #ddd;</span><br><span class="line">    &#125;</span><br><span class="line">    .image &#123;</span><br><span class="line">        border: 1px solid #ddd;</span><br><span class="line">    &#125;</span><br><span class="line">&lt;&#x2F;style&gt;</span><br><span class="line"></span><br><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;div class&#x3D;&quot;container&quot;&gt;</span><br><span class="line">        &lt;div class&#x3D;&quot;header&quot;&gt;</span><br><span class="line">            &lt;span class&#x3D;&quot;title&quot;&gt;数字签名&lt;&#x2F;span&gt;</span><br><span class="line">            &lt;div class&#x3D;&quot;buttons&quot;&gt;</span><br><span class="line">                &lt;button class&#x3D;&quot;save&quot;&gt;保存&lt;&#x2F;button&gt;</span><br><span class="line">                &lt;button class&#x3D;&quot;reset&quot;&gt;重置&lt;&#x2F;button&gt;</span><br><span class="line">            &lt;&#x2F;div&gt;</span><br><span class="line">        &lt;&#x2F;div&gt;</span><br><span class="line">        &lt;div&gt;</span><br><span class="line">            &lt;!-- 一定要设置canvas的宽高，只是通过css设置宽高是不行的会产生偏移 --&gt;</span><br><span class="line">            &lt;canvas class&#x3D;&quot;canvas&quot;  width&#x3D;&quot;600&quot;  height&#x3D; &quot;300&quot;&gt;&lt;&#x2F;canvas&gt;</span><br><span class="line">        &lt;&#x2F;div&gt;</span><br><span class="line">        &lt;div&gt;</span><br><span class="line">            &lt;p&gt;保存的图片为：&lt;&#x2F;p&gt;</span><br><span class="line">            &lt;img class&#x3D;&quot;image&quot;&#x2F;&gt;</span><br><span class="line">        &lt;&#x2F;div&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">    </span><br><span class="line">    &lt;script&gt;</span><br><span class="line">        var save &#x3D; document.querySelector(&#39;.save&#39;);</span><br><span class="line">        var reset &#x3D; document.querySelector(&#39;.reset&#39;)</span><br><span class="line">        var image &#x3D; document.querySelector(&#39;.image&#39;)</span><br><span class="line">        var canvas &#x3D; document.querySelector(&#39;.canvas&#39;)</span><br><span class="line">        var canvasContext &#x3D; canvas.getContext(&#39;2d&#39;)</span><br><span class="line">        var isDraw &#x3D; false;</span><br><span class="line">        var startPos &#x3D; &#123;x: 0,y:0&#125;</span><br><span class="line">        var posArray &#x3D;[];</span><br><span class="line">        canvasContext.strokeStyle &#x3D; &#39;#000&#39;</span><br><span class="line">        canvasContext.lineWidth &#x3D; 1;</span><br><span class="line">        canvasContext.lineJoin &#x3D; &#39;round&#39;;</span><br><span class="line">        canvasContext.lineCap &#x3D; &#39;round&#39;;</span><br><span class="line">        </span><br><span class="line">        canvas.onmousedown &#x3D; function(ev)&#123;</span><br><span class="line">            ev.preventDefault();</span><br><span class="line">            isDraw &#x3D; true;</span><br><span class="line">            startPos &#x3D; &#123;</span><br><span class="line">                x: ev.offsetX,</span><br><span class="line">                y: ev.offsetY</span><br><span class="line">            &#125;</span><br><span class="line">            posArray.push(startPos)</span><br><span class="line">        &#125;</span><br><span class="line">        canvas.onmousemove &#x3D; function(ev)&#123;</span><br><span class="line">            ev.preventDefault();</span><br><span class="line">            if(!isDraw)&#123;</span><br><span class="line">                return;</span><br><span class="line">            &#125;</span><br><span class="line">            var obj &#x3D; &#123;</span><br><span class="line">                x: ev.offsetX,</span><br><span class="line">                y: ev.offsetY</span><br><span class="line">            &#125;</span><br><span class="line">            posArray.push(obj)</span><br><span class="line">            if(posArray.length &gt; 3)&#123;</span><br><span class="line">                var lastTwoPoint &#x3D; posArray.slice(-2);</span><br><span class="line">                var controlPoint &#x3D; lastTwoPoint[0];</span><br><span class="line">                var endPoint &#x3D; &#123;</span><br><span class="line">                    x: (lastTwoPoint[0].x + lastTwoPoint[1].x)&#x2F;2,</span><br><span class="line">                    y: (lastTwoPoint[0].y + lastTwoPoint[1].y)&#x2F;2</span><br><span class="line">                &#125;</span><br><span class="line">                drawLine(startPos, controlPoint, endPoint)</span><br><span class="line">                startPos &#x3D; endPoint;</span><br><span class="line"></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        canvas.onmouseup &#x3D; closeDraw;</span><br><span class="line">        canvas.onmouseout &#x3D; closeDraw;</span><br><span class="line">        function closeDraw(ev)&#123;</span><br><span class="line">            ev.preventDefault();</span><br><span class="line">            isDraw &#x3D; false;</span><br><span class="line">            var obj &#x3D; &#123;</span><br><span class="line">                x: ev.offsetX,</span><br><span class="line">                y: ev.offsetY</span><br><span class="line">            &#125;</span><br><span class="line">            posArray.push(obj)</span><br><span class="line">            if (posArray.length &gt; 3) &#123;</span><br><span class="line">                const lastTwoPoints &#x3D; posArray.slice(-2);</span><br><span class="line">                const controlPoint &#x3D; lastTwoPoints[0];</span><br><span class="line">                const endPoint &#x3D; lastTwoPoints[1];</span><br><span class="line">                drawLine(startPos, controlPoint, endPoint);</span><br><span class="line">            &#125;</span><br><span class="line">            &#x2F;&#x2F; canvasContext.closePath()</span><br><span class="line">            posArray &#x3D; [];</span><br><span class="line">            startPos &#x3D; null;</span><br><span class="line">        &#125;</span><br><span class="line">        &#x2F;&#x2F; 保存成图片</span><br><span class="line">        save.onclick &#x3D; function()&#123;</span><br><span class="line">            console.log(canvas)</span><br><span class="line">            var dataUrl &#x3D; canvas.toDataURL()</span><br><span class="line">            console.log(dataUrl)</span><br><span class="line">            image.src &#x3D; dataUrl;</span><br><span class="line">        &#125;</span><br><span class="line">        &#x2F;&#x2F; 重置</span><br><span class="line">        reset.onclick&#x3D; function()&#123;</span><br><span class="line">            canvasContext.clearRect(0,0,canvas.width,canvas.height)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        function drawLine(startPoint,controlPoint,endPoint)&#123;</span><br><span class="line">             &#x2F;&#x2F; 开始画</span><br><span class="line">            canvasContext.beginPath();</span><br><span class="line">            canvasContext.moveTo(startPoint.x,startPoint.y)</span><br><span class="line">            canvasContext.quadraticCurveTo(controlPoint.x,controlPoint.y,endPoint.x,endPoint.y);</span><br><span class="line">            canvasContext.stroke();</span><br><span class="line">            canvasContext.closePath();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &lt;&#x2F;script&gt;</span><br><span class="line">&lt;&#x2F;body&gt;</span><br><span class="line"></span><br><span class="line">&lt;&#x2F;html&gt;</span><br></pre></td></tr></table></figure>

<p>结果对比：<br><img src="https://fe.che300.com/easymock/upload/2020/03/02/74552325c2f5154fd72713ea7f93e4ce.png"><br><img src="https://fe.che300.com/easymock/upload/2020/03/02/799df615aa8e7f1753d9f95f1815223b.png"></p>
<p>总结：<br>通过上图的对比，我们可以看到锯齿没有完全消除，但是还是优化了好多，<br>希望以后可以找到更好的方法<br>参考：<br><a target="_blank" rel="noopener" href="https://www.jb51.net/html5/641000.html">https://www.jb51.net/html5/641000.html</a></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://gitee.com/weiyayun1995/weiyayun1995.git/2021/06/18/%E6%95%B0%E5%AD%97%E7%AD%BE%E5%90%8D/" data-id="ckq7mlsvj000jdgt91dbn1ndv" data-title="数字签名" class="article-share-link">分享</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2021/06/18/react-hook-%E9%A1%B9%E7%9B%AE%E4%B8%AD%E5%AE%9E%E8%B7%B5/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">前一篇</strong>
      <div class="article-nav-title">
        
          react-hook 项目中实践
        
      </div>
    </a>
  
  
    <a href="/2021/06/18/sendbeacon/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">后一篇</strong>
      <div class="article-nav-title">sendbeacon</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">六月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/05/">五月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/04/">四月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/03/">三月 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/06/22/js%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E8%BD%AC%E6%8D%A2/">js数据类型及转换</a>
          </li>
        
          <li>
            <a href="/2021/06/18/react-hook-%E9%A1%B9%E7%9B%AE%E4%B8%AD%E5%AE%9E%E8%B7%B5/">react-hook 项目中实践</a>
          </li>
        
          <li>
            <a href="/2021/06/18/%E6%95%B0%E5%AD%97%E7%AD%BE%E5%90%8D/">数字签名</a>
          </li>
        
          <li>
            <a href="/2021/06/18/sendbeacon/">sendbeacon</a>
          </li>
        
          <li>
            <a href="/2021/06/18/js%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%88%A4%E6%96%AD/">js数据类型的判断</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 yywei<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>